<?php
	
	include("maintenance_config.inc");

	$link = mysql_connect($dbhost, $dbuser, $dbpass);
	mysql_select_db($dbname);

	$query0 = "SELECT * FROM cluster";
	$buffer = mysql_query($query0);
	while( $ip[] = mysql_fetch_assoc($buffer) );
	unset( $ip[mysql_num_rows($buffer)] );
	//print_r($ip);

	$cve_inserted = 0;
	if( $count_entries > 0 ){
		foreach( $cve as $cvename => $value ){

			//check each cvename is new or was stored on which cluster

			//new cve
			//determine that which cluster cve will be sent to
			
			//now we ignore 2 case above bcoz we have only one cluster
			
			$fp = fsockopen(''.$ip[0]['IP'],$socketport,$errno,$errstr,30);
			if (!is_resource($fp)) {
				exit("connection fail: ".$errnum." ".$errstr) ;
			}
			else{
				//print_r($value);
				$input = json_encode($value);
				//echo $input."<br><br>";
				@fputs($fp, $input);
				fclose($fp);
			}

		}
	}
	echo("<script>window.open('', '_self', ''); window.close();</script>");
			/*mysql_connect( $ip[$i]['IP'], $clusteruser, $clusterpass ) or die("cant connect to ".$ip[$i]['IP']);
			mysql_select_db($cluster_dbname);
			for( $j=$i ; $j<$count_entries ; $j+=$clusternum ){
				//print_r($cve[$j]);
				$value = $cve[$j];
				$cvename = $value['cvename'];
				if( $value['CVSS'] == 0 || $value['CVSS'] == '' || $value['CVSS'] == null ){
					$query = "REPLACE INTO cveinfo (cvename, published, year, description, severity) VALUE ";
					$query .= "('".$cvename."', '".$value['published']."', '".$value['year']."', '".$value['description']."', '";
					$query .= $value['severity']."')";
					//echo $query;
				}
				else{
					$query = "REPLACE INTO cveinfo (cvename, published, year, description, severity, CVSS, cvss_exp, cvss_imp) VALUE ";
					$query .= "('".$cvename."', '".$value['published']."', '".$value['year']."', '".$value['description']."', '";
					$query .= $value['severity']."', ".$value['CVSS'].", ".$value['cvss_exp'].", ".$value['cvss_imp'].")";
				}
				if( $buffer = mysql_query($query) ){
					$cve_inserted++;
					echo "insert $cvename to cveinfo.<br>";
					if( isset($value['vendor']) ){
						foreach( $value['vendor'] as $index => $item ){
							$query = "REPLACE INTO effect (cvename, vendor, product, versions) VALUE ";
							$query .= "('".$cvename."', '".$item."', '".$cve[$j]['product'][$index]."', '".$cve[$j]['versions'][$index]."')";
							if( $buffer2 = mysql_query($query) )
								echo "insert $cvename to effect.<br>";
							else
								echo "<b>insert $cvename to effect error.</b><br>";
						}
					}
				}
				else{
					echo "<b>insert $cvename to cveinfo error.</b><br>";
					echo mysql_error($link).'<br>';
				}
				echo "----------------<br>";
			}
		}
	}

	echo "insert totally $cve_inserted cve to cveinfo.<br>";*/
?>